package com.tailyou.hms.firstapp.act;

import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.core.view.GravityCompat;
import androidx.drawerlayout.widget.DrawerLayout;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.google.android.material.navigation.NavigationView;
import com.google.android.material.snackbar.Snackbar;
import com.tailyou.hms.firstapp.R;
import com.tailyou.hms.firstapp.adapter.RVAdapter;
import com.tailyou.hms.firstapp.bean.RVBean;

import java.util.ArrayList;
import java.util.List;

public class MaterialDesignActivity extends AppCompatActivity {

    private Toolbar toolbar;
    private DrawerLayout drawerLayout;
    private NavigationView navView;
    private FloatingActionButton fabDelete;
    private RecyclerView recycleView;
    private SwipeRefreshLayout swipeRefreshLayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_material_design);
        toolbar = findViewById(R.id.toolbar);
        drawerLayout = findViewById(R.id.drawerLayout);
        navView = findViewById(R.id.navView);
        recycleView = findViewById(R.id.recycleView);
        fabDelete = findViewById(R.id.fabDelete);
        swipeRefreshLayout = findViewById(R.id.swipeRefreshLayout);
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setHomeAsUpIndicator(R.mipmap.ic_menu);
        navView.setCheckedItem(R.id.set);
        navView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                drawerLayout.closeDrawers();
                return true;
            }
        });
        fabDelete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                // 删除按钮的响应，执行删除操作，并通过SnackBar提示删除成功
                Snackbar.make(view, "点击了删除按钮，执行删除操作", Snackbar.LENGTH_LONG).setAction("撤销", new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        Toast.makeText(MaterialDesignActivity.this, "撤销删除操作成功", Toast.LENGTH_SHORT).show();
                    }
                }).show();
            }
        });
        recycleView.setLayoutManager(new GridLayoutManager(MaterialDesignActivity.this, 2));
        List<RVBean> datas = new ArrayList<>();
        datas.add(new RVBean(R.mipmap.pc1, "" + R.mipmap.pc1));
        datas.add(new RVBean(R.mipmap.pc2, "" + R.mipmap.pc2));
        datas.add(new RVBean(R.mipmap.pc3, "" + R.mipmap.pc3));
        datas.add(new RVBean(R.mipmap.pc4, "" + R.mipmap.pc4));
        datas.add(new RVBean(R.mipmap.pc5, "" + R.mipmap.pc5));
        datas.add(new RVBean(R.mipmap.pc6, "" + R.mipmap.pc6));
        datas.add(new RVBean(R.mipmap.pc8, "" + R.mipmap.pc8));
        datas.add(new RVBean(R.mipmap.pc9, "" + R.mipmap.pc9));
        datas.add(new RVBean(R.mipmap.pc1, "" + R.mipmap.pc1));
        datas.add(new RVBean(R.mipmap.pc2, "" + R.mipmap.pc2));
        datas.add(new RVBean(R.mipmap.pc3, "" + R.mipmap.pc3));
        datas.add(new RVBean(R.mipmap.pc4, "" + R.mipmap.pc4));
        datas.add(new RVBean(R.mipmap.pc5, "" + R.mipmap.pc5));
        datas.add(new RVBean(R.mipmap.pc6, "" + R.mipmap.pc6));
        datas.add(new RVBean(R.mipmap.pc8, "" + R.mipmap.pc8));
        datas.add(new RVBean(R.mipmap.pc9, "" + R.mipmap.pc9));
        recycleView.setAdapter(new RVAdapter(datas) {
            @Override
            public void clickItem(int position) {
                startActivity(new Intent(MaterialDesignActivity.this, CTLActivity.class));
            }
        });
        swipeRefreshLayout.setOnRefreshListener(() -> {
            new Thread(() -> {
                try {
                    Thread.sleep(2000);
                    runOnUiThread(() -> swipeRefreshLayout.setRefreshing(false));
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }).start();
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.toolbar_menu, menu);
        for (int i = 0; i < menu.size(); i++) {
            MenuItem item = menu.getItem(i);
            Drawable drawable = item.getIcon();
            if (drawable != null) {
                int size = getResources().getDimensionPixelSize(R.dimen.menu_icon_size);
                drawable.setBounds(0, 0, size, size);
                item.setIcon(drawable);
            }
        }
        return true;
    }

    //菜单的点击事件
    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                drawerLayout.openDrawer(GravityCompat.START);
                break;
            case R.id.upload:
                Toast.makeText(this, "上传", Toast.LENGTH_SHORT).show();
                break;
            case R.id.delete:
                Toast.makeText(this, "删除", Toast.LENGTH_SHORT).show();
                break;
            case R.id.back:
                Toast.makeText(this, "退出", Toast.LENGTH_SHORT).show();
                break;
            case R.id.add:
                Toast.makeText(this, "添加", Toast.LENGTH_SHORT).show();
                break;
            default:
                super.onOptionsItemSelected(item);
        }
        return true;
    }

}